<template>
  <div class="dashboard-layout">
    <el-container>
      <el-header>
        <div class="logo-box">
          <el-icon class="collapse-icon" @click="handleChangeMenuCollapse">
            <i class="iconfont icon-liebiao"></i>
          </el-icon>
          <el-divider direction="vertical" />
          <span class="logo-text" @click="handleGotoIndex">{{$t('appName')}}</span>
        </div>
<!--        <div class="search-box">-->
<!--          <el-input-->
<!--              clearable-->
<!--              class="header-search-input"-->
<!--              v-model="searchText"-->
<!--              size="large"-->
<!--              placeholder="输入关键字搜索"-->
<!--              :suffix-icon="Search"-->
<!--          />-->
<!--        </div>-->
        <div class="user-box">
          <el-badge :value="6" class="notice-badge">
            <i class="iconfont icon-xiaoxitixing"></i>
          </el-badge>
          <el-divider direction="vertical" />
          <el-dropdown @command="handleDropMenuClick">
            <div class="user-info">
              <div class="avatar-bg">
                <div class="avatar" :style="{'background-image': 'url('+userInfo.icon+')'}"></div>
              </div>
              <span class="name">{{userInfo.nickname}}</span>
              <el-icon class="el-icon--right">
                <arrow-down />
              </el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="info-change">个人信息修改</el-dropdown-item>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      <el-container>
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <el-menu
              router
              :default-active="currentPath"
              class="el-menu-vertical-demo"
              :collapse="isCollapse">
            <el-menu-item index="/ds/pcenter">
              <el-icon>
                <i class="iconfont icon-wode"></i>
              </el-icon>
              <template #title>{{$t('menu.myInfo')}}</template>
            </el-menu-item>
            <el-menu-item index="/ds/sci-hot">
              <el-icon>
                <i class="iconfont icon-hotfill"></i>
              </el-icon>
              <template #title>{{$t('menu.scientificHot')}}</template>
            </el-menu-item>
            <el-menu-item index="/ds/ai-search">
              <el-icon>
                <i class="iconfont icon-sousuo"></i>
              </el-icon>
              <template #title>{{$t('menu.aiSearch')}}</template>
            </el-menu-item>
          </el-menu>
        </el-aside>
        <el-main class="dashboard-main"><router-view/></el-main>
      </el-container>
      <el-footer>
        <footer-comp></footer-comp>
      </el-footer>
    </el-container>
  </div>
  <el-dialog title="用户信息修改" width="30%" v-model="userInfoDialogVisible">
    <div class="user-info-content">
      <div class="user-avatar-content">
        <div class="avatar-box">
          <div class="avatar" :style="{'background-image': 'url('+userInfoForm.icon+')'}"></div>
          <div class="avatar-mask">
            <i class="iconfont icon-shangchuantouxiang"></i>
          </div>
          <input class="file-input" type="file" accept="image/*" @change="handleFileChange"/>
        </div>
      </div>
      <div class="form-row">
        <span class="label">昵称</span>
        <el-input placeholder="请输入昵称" v-model="userInfoForm.nickname"></el-input>
      </div>
    </div>
    <template #footer>
      <div>
        <el-button link @click="userInfoDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSaveUserInfo">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import {ref, provide} from 'vue'
import {Fold, Expand, ArrowDown, Search } from '@element-plus/icons-vue'
import FooterComp from './components/footer.vue'
import {useI18n} from "vue-i18n";
import {useRouter} from "vue-router";
import api from './api/index.js'
import common from "~/common/common.js";

const user = common.getLocalUser()
const uid = user.userId
const router = useRouter()
const isCollapse = ref(false)
const handleChangeMenuCollapse = () => {
  isCollapse.value = !isCollapse.value
}

const searchText = ref('')

const currentPath = router.currentRoute.value.fullPath

const userInfoForm = ref({
  nickname: '',
  icon: '',
})
const userInfo = ref({
  nickname: '',
  icon: '',
})
provide('userInfo', userInfo)
const getUserInfo = async () => {
  try {
    const res = await api.getUserInfo({uid})
    if (res && res.uid) {
      userInfo.value.nickname = res.nickname
      userInfo.value.icon = 'data:image/png;base64,' + res.icon
      userInfoForm.value.nickname = res.nickname
      userInfoForm.value.icon = 'data:image/png;base64,' + res.icon
    }
  } catch(e) {
    console.log(e)
  }
}

const handleGotoIndex = () => {
  router.replace('/index')
}

const userInfoDialogVisible = ref(false)
const handleDropMenuClick = (command) => {
  switch(command) {
    case 'info-change':
      getUserInfo()
      userInfoDialogVisible.value = true
      break;
    case 'logout':
      common.removeLocalUser()
      common.removeLocalToken()
      router.replace('/login')
      break;
  }
}
let avatarFile = null
const handleFileChange = (evt) => {
  const file = evt.target.files[0]
  avatarFile = file
  let fileResult = ''
  const reader = new FileReader()
  reader.readAsDataURL(file)
  reader.onload = () => {
    fileResult = reader.result
  }
  reader.onloadend = () => {
    userInfoForm.value.icon = fileResult
    // console.log('handleFileChange', fileResult)
  }
}
const saveUserInfoLoading = ref(false)
const handleSaveUserInfo = () => {
  // if (!userInfoForm.value.icon || !userInfoForm.value.nickname) {
  //   ElMessage.error('请补充头像和昵称信息')
  //   return
  // }
  saveUserInfoLoading.value = true
  const formData = new FormData();
  if (avatarFile) {
    formData.append('icon', avatarFile)
  }
  api.createUserInfo({
    uid,
    nickname: userInfoForm.value.nickname
  }, formData).then(res => {
    saveUserInfoLoading.value = false
    userInfoDialogVisible.value = false
    if (res.status === 'success') {
      userInfo.value.nickname = userInfoForm.value.nickname
      userInfo.value.icon = userInfoForm.value.icon
      ElMessage.success('保存成功')
    } else {
      ElMessage.error('保存失败')
    }
  }).catch(e => {
    saveUserInfoLoading.value = false
    ElMessage.error('保存异常')
    console.log(e)
  })
}
getUserInfo()
</script>

<style scoped lang="scss">
  .dashboard-layout{
    position: relative;
    width: 100%;
    height: 100%;
  }
  .el-container{
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
  }
  .el-header{
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 80px;
    .logo-box{
      display: flex;
      align-items: center;
      font-size: 2.4rem;
    }
    .logo-text{
      cursor: pointer;
    }
    .collapse-icon{
      cursor: pointer;
    }
    .icon-liebiao{
      font-size: 2.2rem;
    }
    .header-search-input{
      width: 32rem;
    }
    .el-tooltip__trigger{
      &:focus-visible{
        outline: none;
        border: none;
      }
    }
  }
  .el-aside{
    //background: rgb(60 64 73);
  }
  .el-menu{
    height: 100%;
    border-right: none;
    //background: none;
    background: rgb(60 64 73);
  }
  .el-menu-item{
    color: #fff;
    &:hover{
      background-color: rgb(56 159 238);
    }
    &.is-active{
      color: #fff;
      background-color: rgb(56 159 238);
    }
  }
  .user-box{
    display: flex;
    align-items: center;
    justify-content: space-between;

    .icon-xiaoxitixing{
      font-size: 2.4rem;
      color: var(--el-text-color-secondary);
    }
    .el-divider{
      margin: 0 20px;
    }
  }
  .dashboard-main{
    background: #f4f5f7;
  }
  .user-info{
    position: relative;
    display: flex;
    align-items: center;
    cursor: pointer;
    .avatar-bg{
      position: relative;
      width: 3rem;
      height: 3rem;
      border-radius: 3rem;
      background: #f7f7f7;
    }
    .avatar{
      width: 100%;
      height: 100%;
      border-radius: 3rem;
      background-size: 100% 100%;
    }
    .name{
      margin: 0 10px;
    }
  }
  .user-info-content{
    position: relative;
    //padding: 2rem 3.5rem;
  }
  .user-avatar-content{
    position: relative;
    display: flex;
    justify-content: center;
  }
  .avatar-box {
    position: relative;
    width: 16rem;
    height: 16rem;
    min-height: 16rem;
    background: #f7f7f7;
    border-radius: 16rem;
    cursor: pointer;

    .avatar {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      border-radius: 16rem;
      background-repeat: no-repeat;
      background-size: 100% 100%;
    }

    .avatar-mask {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      border-radius: 16rem;
      background: rgba(0, 0, 0, 0.1);
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      opacity: 0;

      .iconfont {
        font-size: 3.0rem;
        color: #fff;
      }
    }

    .file-input {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      border-radius: 16rem;
      opacity: 0;
      cursor: pointer;
    }

    &:hover {
      .avatar-mask {
        opacity: 1;
      }
    }
  }
  .form-row{
    position: relative;
    margin-top: 20px;
    display: flex;
    align-items: center;
    .label{
      width: 80px;
      min-width: 80px;
    }
  }
  .el-footer{
    height: 80px;
  }
</style>
